/**
 */
package com.dingpeikeji.quickdeer.core.modules.finance.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.activity.entity.ActivityInfo;
import com.dingpeikeji.quickdeer.core.modules.activity.service.TActivityService;
import com.dingpeikeji.quickdeer.core.modules.bus.entity.TCompBus;
import com.dingpeikeji.quickdeer.core.modules.bus.service.TCompBusService;
import com.dingpeikeji.quickdeer.core.modules.exchg.entity.TBusExchg;
import com.dingpeikeji.quickdeer.core.modules.exchg.service.TBusExchgService;
import com.dingpeikeji.quickdeer.core.modules.finance.entity.TFinance;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 财务管理Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/tFinance")
public class TFinanceController extends BaseController {
	
	@Autowired
	private TOrdersService tOrdersService;
	
	@RequestMapping(value = { "list", "" })
	public String list(TFinance tFinance, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<TFinance> page = tOrdersService.findFinanceList(new Page<TFinance>(request, response), tFinance);
		model.addAttribute("page", page);
		return "modules/finance/tFinanceList";
	}
	
	@RequestMapping(value = "form")
	public String form(TFinance tFinance, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		tFinance = tOrdersService.getFinance(tFinance);
		model.addAttribute("tFinance", tFinance);
		return "modules/finance/tFinanceForm";
	}
	
	@RequestMapping(value = "save")
	public String save(TFinance tFinance, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		if(!tFinance.getOrdertype().equals("7")){
			tFinance.setSettlement("1");
		}
		if(tFinance.getDeductions()==null){
			tFinance.setDeductions(0.00);
		}
		else if(tFinance.getNowpayget()+tFinance.getDownpay()+tFinance.getOwnpayget()>=(Double.parseDouble(tFinance.getDueamount())-tFinance.getDeductions())){
			tFinance.setSettlement("1");
		}else{
			tFinance.setSettlement("0");
		}
		tOrdersService.saveFinance(tFinance);
		model.addAttribute("message", "保存成功");
		return "redirect:list";
	}

}
